<?php

class login extends picMakeupEr {

    public function index( $c ) {

        if ( $this->admin ) {
            if ( G( 'referer' ) ) {
                header( "location:" . G( 'referer' ) );
            } else {
                header( "location:?c=home" );
            }
        }
        include $this->display( $c );
    }

    public function getcode() {
        $this->Image()->Verify();
    }

    public function chkLogin() {

        if ( isset( $_POST[ 'verify' ] ) && strtolower( $_POST[ 'verify' ] ) != $_SESSION[ "verify" ] ) {
            exit( '验证码错误!' );
        }
        $author = trim( $_POST[ 'author' ] );
        $pass = md6( trim( $_POST[ 'pass' ] ) );

        $date = array( 'author' => $author );
        $this->table = "user";
        if ( !$result = $this->find( $date ) ) {
            addlog( 'login', $author . "==" . getRequest( "ip" ) . "==账失败：号不存在" );
            exit( "账号不存在!" );
        }

        if ( $result[ 'pass' ] != $pass ) {
            addlog( 'login', $author . "==" . getRequest( "ip" ) . "==失败：密码错误" );
            exit( " 密码错误!" );
        }

        if ( $result[ 'status' ] ) {
            addlog( 'login', $author . "==" . getRequest( "ip" ) . "==失败：账号已被锁定" );
            exit( "账号已被锁定!" );
        }

        $this->table = "user";
        $this->update( $date, array( 'lastdate' => time(), 'lastip' => getRequest( 'ip' ) ) );

        $expire = 0;
        if ( isset( $_POST[ 'remember' ] ) ) {
            $expire = empty( $this->setting[ 'cookie_time' ] ) ? $_POST[ 'remember' ] : ( $this->setting[ 'cookie_time' ] * 3600 * 24 + time() );
        }

        addlog( 'login', $author . "==" . getRequest( "ip" ) . "==成功" );
        $cookie_value = "{$result['author']}|{$result['id']}|{$expire}";
        $cookie_value = md6( $cookie_value, 'encode' );
        setcookie( WEBCOOKIE, $cookie_value, $expire, '/' );
        exit( "success" );
    }

    public function logout() {
        setcookie( WEBCOOKIE, '', time() - ( $this->setting[ 'cookie_time' ] * 3600 * 24 ), '/' );
        header( "Location: ../" );
    }
}